package main

import (
	"encoding/json"
	"fronter/config"
	"fronter/svrConnectMgr"

	"github.com/lesismal/arpc"
	"github.com/sirupsen/logrus"
)

var server *svrConnectMgr.MyServer

func handle_echo(ctx *arpc.Context) {
	req_body := ""
	if err := ctx.Bind(&req_body); err == nil {
		logrus.Infoln("req body:", req_body)
		ctx.Write(req_body)
	}
}

func handle_login(ctx *arpc.Context) {
	req_body := ""
	if err := ctx.Bind(&req_body); err == nil {
		var req svrConnectMgr.ProtoUserLoginReq
		var res svrConnectMgr.ProtoUserLoginRes
		json.Unmarshal([]byte(req_body), &req)
		logrus.Infoln("login req:", req)
		if req.Account == "admin" && req.Pwd == "123456" {
			res.Rescode = 200
			logrus.Infoln("login success!")
		} else {
			res.Rescode = 502
			logrus.Infoln("login failed!")
		}
		res_str, _ := json.Marshal(res)
		logrus.Infoln("res str:", string(res_str))
		ctx.Write(string(res_str))
	}
}

func handle_register(ctx *arpc.Context) {
	req_body := ""
	if err := ctx.Bind(&req_body); err == nil {
		var req svrConnectMgr.ProtoUserRegReq
		var res svrConnectMgr.ProtoUserRegRes
		json.Unmarshal([]byte(req_body), &req)
		logrus.Infoln("reg req:", req)

	}
}

func init() {
	server = svrConnectMgr.GetSingleSvr()
	logrus.Infoln("path init.")
	// register router
	server.Route("/echo", handle_echo)
	server.Route("/login", handle_login)
	server.Route("/register", handle_register)
}

func main() {
	logrus.Infoln(server.ArpcSvr)
	server.Run(config.Get().SvrIp, config.Get().SvrPort)
}
